package com.situ.hospital.doctor.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.hospital.doctor.dao.IDoctorDao;
import com.situ.hospital.doctor.model.DoctorModel;

import util.IsEmpty;
import util.JDBCUtil;

public class DoctorDaoImpl implements IDoctorDao {
	@Override
	public Integer insert(DoctorModel doctor) {
		String sql = "insert into doctor(doctor_id,doctor_name,doctor_age,doctor_title,department_name) values(?,?,?,?,?)";
		return JDBCUtil.dml(sql, doctor.getDoctorId(), doctor.getDoctorName(), doctor.getDoctorAge(),
				doctor.getDoctorTitle(), doctor.getDepartmentName());
	}

	@Override
	public DoctorModel selectModel(DoctorModel doctor) throws SQLException {
		String sql = "select * from doctor where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, doctor, 2);
		List<DoctorModel> backlist = JDBCUtil.dql(sql, list, DoctorModel.class, doctor.mapGet());
		if (backlist == null || backlist.size() == 0) {
			return null;
		} else {
			return backlist.get(0);
		}
	}

	@Override
	public List<DoctorModel> selectAll(DoctorModel doctor) throws SQLException {
		String sql = "select * from doctor where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, doctor, 2);
		
		return JDBCUtil.dql(sql, list, DoctorModel.class, doctor.mapGet());
	}

	@Override
	public Integer deleteById(DoctorModel doctor) throws SQLException {
		String sql = "delete from doctor where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, doctor, 2);
		return JDBCUtil.dml(sql, list);
	}

	@Override
	public Integer updateById(DoctorModel doctor) throws SQLException {
		String sql = "update doctor set ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, doctor, 3);
		return JDBCUtil.dml(sql, list);
	}
	
	@Override
	public Integer updateJpg(DoctorModel doctor) throws SQLException {
		String sql = "update doctor set doctor_jpg=? where doctor_id=?";
		List<Object> list=new ArrayList<>();
		list.add(null);
		list.add(doctor.getDoctorId());
		return JDBCUtil.dml(sql, list);
	}
	

	@Override
	public Integer selectCount(DoctorModel doctor) throws SQLException {
		String sql = "select count(1) from doctor where ";
		List<Object> list = new ArrayList<>();
		sql = setSql(sql, list, doctor, 2);
		return JDBCUtil.selectCount(sql, list);

	}

	/**
	 * @param sql
	 * @param list
	 * @param department
	 * @param n          1为insert 2为select 2为delete 3为update
	 * @return
	 */
	private String setSql(String sql, List<Object> list, DoctorModel doctor, int m) {
		int n = 0;

		if (doctor.getId() != null && doctor.getId() == 1) {
			sql += "doctor_name like ? and doctor_age like ? and doctor_title like ? and department_name like ?";
			
			list.add(doctor.getDoctorName());
			list.add(doctor.getDoctorAge());
			list.add(doctor.getDoctorTitle());
			list.add(doctor.getDepartmentName());
			if (doctor.isPageOn()) {
				sql += " limit ?,?";
				list.add(doctor.getRowStart());
				list.add(doctor.getPageLimit());
			}
			return sql;
		}
		Integer id = doctor.getDoctorId();
		if (!IsEmpty.isEmpty(id) && m != 3) {
			if (n == 1) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "doctor_id=?";
			list.add(id);
			n = m;
		}
		String name = doctor.getDoctorName();
		if (!IsEmpty.isEmpty(name)) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "doctor_name=?";
			list.add(name);
			n = m;
		}
		String age = doctor.getDoctorAge();
		if (!IsEmpty.isEmpty(age)) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "doctor_age=?";
			list.add(age);
			n = m;
		}
		String title = doctor.getDoctorTitle();
		if (!IsEmpty.isEmpty(title)) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "doctor_title=?";
			list.add(title);
			n = m;
		}
		String departmentName = doctor.getDepartmentName();
		if (!IsEmpty.isEmpty(departmentName)) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "department_name=?";
			list.add(departmentName);
			n = m;
		}
		String jpg=doctor.getDoctorJpg();
		if (!IsEmpty.isEmpty(jpg)&&m!=3) {
			if (n == 1 || n == 3) {
				sql += ",";
			} else if (n == 2) {
				sql += " and ";
			}
			sql += "doctor_jpg=?";
			list.add(jpg);
			n = m;
		}
		if (m == 3) {
			sql += "where doctor_id=?";
			list.add(id);
		}

		return sql;
	}
}
